package com.yunao.service.report.esou.Jfreechart;

import com.yunao.service.report.esou.ChartUtils;
import com.yunao.service.report.esou.Serie;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.Layer;
import org.jfree.ui.RectangleEdge;

import java.awt.*;
import java.util.Vector;


/**
 * 
 * @author ccw
 * @date 2014-6-11
 *       <p>
 *       ����ͼ�?�裺<br/>
 *       1��������ݼ���<br/>
 *       2������Chart��<br/>
 *       3:���ÿ���ݣ���ֹ������ʾ�����<br/>
 *       4:�����ӽ�����Ⱦ��<br/>
 *       5:������ֽ�����Ⱦ<br/>
 *       6:ʹ��chartPanel����<br/>
 * 
 *       </p>
 */
public class LineChart {
	public LineChart() {
	}

	public JFreeChart createChartLine(String title , String xName , String yName , String row[] , String xAxis[] , Double data[][]) {
		JFreeChart chart = ChartFactory.createLineChart(title, xName , yName, createDataset(row , xAxis , data), null, false, false, false);
		ChartUtils.setAntiAlias(chart);
		CategoryPlot plot = chart.getCategoryPlot();
		plot.setRangeGridlinesVisible(false);
		LineAndShapeRenderer lasp = (LineAndShapeRenderer) plot.getRenderer(); 
		lasp.setSeriesPaint(11, new Color(69,114,167));
		lasp.setSeriesPaint(0, new Color(170,70,67));
		lasp.setSeriesPaint(1, new Color(69,114,167));
		lasp.setSeriesPaint(2, new Color(137,165,78));
		lasp.setSeriesPaint(3, new Color(128,105,155));
		lasp.setSeriesPaint(4, new Color(61,150,174));
		lasp.setSeriesPaint(5, new Color(219,132,61));
		lasp.setSeriesPaint(6, new Color(146,168,205));
		lasp.setSeriesPaint(7, new Color(164,125,124));
		lasp.setSeriesPaint(8, new Color(181,202,146));
		lasp.setSeriesPaint(9, new Color(187,129,78));
		lasp.setSeriesPaint(10, new Color(69,114,167));

		
		lasp.setSeriesStroke(0, new BasicStroke(2F));
		lasp.setSeriesStroke(1, new BasicStroke(2F));
		lasp.setSeriesStroke(2, new BasicStroke(2F));
		lasp.setSeriesStroke(3, new BasicStroke(2F));
		lasp.setSeriesStroke(4, new BasicStroke(2F));
		lasp.setSeriesStroke(5, new BasicStroke(2F));
		lasp.setSeriesStroke(6, new BasicStroke(2F));
		lasp.setSeriesStroke(7, new BasicStroke(2F));
		lasp.setSeriesStroke(8, new BasicStroke(2F));
		lasp.setSeriesStroke(9, new BasicStroke(2F));
		lasp.setSeriesStroke(10, new BasicStroke(2F));
		lasp.setSeriesStroke(11, new BasicStroke(2F));
		ChartUtils.setLineRender(plot, false,true);//
		ChartUtils.setXAixs(plot);
		ChartUtils.setYAixs(plot);
//		for (int i = 0; i < xAxis.length; i++) {
//			System.out.println("xAxis是："+xAxis[i]);
//		}
		int temp = 0 ;
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[i].length; j++) {
				if(temp<(int)(double)data[i][j]){
					temp = (int)(double)data[i][j];
				}
			}		
		}
		chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
		chart.getLegend().setPosition(RectangleEdge.TOP);
		if(temp<2000&&temp>400){
			IntervalMarker intermarker  = new IntervalMarker(0,100);  
			IntervalMarker intermarker1 = new IntervalMarker(200,300);
			IntervalMarker intermarker2 = new IntervalMarker(400,500);
			IntervalMarker intermarker3 = new IntervalMarker(600,700);
			IntervalMarker intermarker4 = new IntervalMarker(800,900);
			IntervalMarker intermarker5 = new IntervalMarker(1000,1100);
			IntervalMarker intermarker6 = new IntervalMarker(1200,1300);
			IntervalMarker intermarker7 = new IntervalMarker(1400,1500);
			IntervalMarker intermarker8 = new IntervalMarker(1600,1700);
			intermarker.setPaint(Color.decode("#efefef"));// 域顏色
			intermarker1.setPaint(Color.decode("#efefef"));
			intermarker2.setPaint(Color.decode("#efefef"));
			intermarker3.setPaint(Color.decode("#efefef"));
			intermarker4.setPaint(Color.decode("#efefef"));
			intermarker5.setPaint(Color.decode("#efefef"));
			intermarker6.setPaint(Color.decode("#efefef"));
			intermarker7.setPaint(Color.decode("#efefef"));
			intermarker8.setPaint(Color.decode("#efefef"));
			plot.addRangeMarker(intermarker, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker1, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker2, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker3, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker4, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker5, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker6, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker7, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker8, Layer.BACKGROUND);
		}else if(temp>2000&&temp<4000){
			IntervalMarker intermarker  = new IntervalMarker(0,300);  
			IntervalMarker intermarker1 = new IntervalMarker(600,900);
			IntervalMarker intermarker2 = new IntervalMarker(1200,1500);
			IntervalMarker intermarker3 = new IntervalMarker(1800,2100);
			IntervalMarker intermarker4 = new IntervalMarker(2400,2700);
			IntervalMarker intermarker5 = new IntervalMarker(3000,3300);
			IntervalMarker intermarker6 = new IntervalMarker(3600,3900);
			IntervalMarker intermarker7 = new IntervalMarker(4200,4500);
			IntervalMarker intermarker8 = new IntervalMarker(4800,5100);
			intermarker.setPaint(Color.decode("#efefef"));// 域顏色
			intermarker1.setPaint(Color.decode("#efefef"));
			intermarker2.setPaint(Color.decode("#efefef"));
			intermarker3.setPaint(Color.decode("#efefef"));
			intermarker4.setPaint(Color.decode("#efefef"));
			intermarker5.setPaint(Color.decode("#efefef"));
			intermarker6.setPaint(Color.decode("#efefef"));
			intermarker7.setPaint(Color.decode("#efefef"));
			intermarker8.setPaint(Color.decode("#efefef"));
			plot.addRangeMarker(intermarker, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker1, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker2, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker3, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker4, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker5, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker6, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker7, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker8, Layer.BACKGROUND);
		}else if(temp>100&&temp<400){
			IntervalMarker intermarker  = new IntervalMarker(0,30);  
			IntervalMarker intermarker1 = new IntervalMarker(60,90);
			IntervalMarker intermarker2 = new IntervalMarker(120,150);
			IntervalMarker intermarker3 = new IntervalMarker(180,210);
			IntervalMarker intermarker4 = new IntervalMarker(240,270);
			IntervalMarker intermarker5 = new IntervalMarker(300,330);
			IntervalMarker intermarker6 = new IntervalMarker(360,390);
			IntervalMarker intermarker7 = new IntervalMarker(420,450);
			//IntervalMarker intermarker8 = new IntervalMarker(1600,1700);
			intermarker.setPaint(Color.decode("#efefef"));// 域顏色
			intermarker1.setPaint(Color.decode("#efefef"));
			intermarker2.setPaint(Color.decode("#efefef"));
			intermarker3.setPaint(Color.decode("#efefef"));
			intermarker4.setPaint(Color.decode("#efefef"));
			intermarker5.setPaint(Color.decode("#efefef"));
			intermarker6.setPaint(Color.decode("#efefef"));
			intermarker7.setPaint(Color.decode("#efefef"));
			//intermarker8.setPaint(Color.decode("#efefef"));
			plot.addRangeMarker(intermarker, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker1, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker2, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker3, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker4, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker5, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker6, Layer.BACKGROUND);
			plot.addRangeMarker(intermarker7, Layer.BACKGROUND);
			//plot.addRangeMarker(intermarker8, Layer.BACKGROUND);
		}else{
			
		}
		return chart;
	}
	public JFreeChart createChartLine1(String title , String xName , String yName , String row[] , String xAxis[] , Double data[][]) {
		JFreeChart chart = ChartFactory.createLineChart(title, xName , yName, createDataset(row , xAxis , data));
		ChartUtils.setAntiAlias(chart);
		CategoryPlot plot = chart.getCategoryPlot();
		plot.setOutlinePaint(Color.WHITE);
		
		LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
		renderer.setSeriesStroke(0, new BasicStroke(2F));
		renderer.setSeriesStroke(1, new BasicStroke(2F));
		renderer.setSeriesStroke(2, new BasicStroke(2F));
		renderer.setSeriesStroke(3, new BasicStroke(2F));
		renderer.setSeriesStroke(4, new BasicStroke(2F));
		renderer.setSeriesStroke(5, new BasicStroke(2F));
		renderer.setSeriesPaint(0, new Color(69,114,167));
		renderer.setSeriesPaint(1, new Color(170,70,67));
		renderer.setSeriesPaint(2, new Color(137,165,78));
		renderer.setSeriesPaint(3, new Color(128,105,155));
		renderer.setSeriesPaint(4, new Color(61,150,174));
		renderer.setSeriesPaint(5, new Color(219,132,61));



		ChartUtils.setLineRender(plot, false,true);//
		ChartUtils.setXAixs(plot);
		ChartUtils.setYAixs(plot);
		chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
		chart.getLegend().setPosition(RectangleEdge.TOP);
		
		return chart;
	}
	
	public DefaultCategoryDataset createDataset(String row[] , String xAxis[] , Double data[][]) {
	
		Vector<Serie> series = new Vector<Serie>();
		for (int i = 0; i < row.length; i++) {
			if(row[i] == null)
				break;
			series.add(new Serie(row[i], data[i]));
		}
		DefaultCategoryDataset dataset = ChartUtils.createDefaultCategoryDataset(series, xAxis);
		return dataset;
	}

}
